package Code.Day8_20240925.com.dx.day08.utils.com.dx.xjwlmq.dao;

import Code.Day8_20240925.com.dx.day08.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName SubjectDaoImpl
 * @Description 科目表基本DAO操作封装
 * @Author 12994
 * @Date 2024/9/26 17:09
 * @Version 1.0
 **/
public class SubjectDaoImpl extends BaseDao implements SubjectBaseDao {
    @Override
    public ArrayList<Subject> getSubjectList() {
        ArrayList<Subject> subjects = new ArrayList<>();
        Subject thisSubject = null;
        try {
            this.rs = baseQuery("SELECT * FROM subject;");
            while (this.rs.next()) {
                thisSubject = new Subject();
                thisSubject.setSubjectNo(this.rs.getInt("SubjectNo"));
                thisSubject.setSubjectName(this.rs.getString("SubjectName"));
                thisSubject.setClassHour(this.rs.getInt("ClassHour"));
                thisSubject.setGradeID(this.rs.getInt("GradeID"));
                subjects.add(thisSubject);
            }
        } catch (SQLException e) {
            logger.warn(String.valueOf(e.fillInStackTrace()));
        }
        return subjects;
    }

    @Override
    public int addSubject(Subject subject) {
        return baseUpdate(
                "INSERT INTO subject (SubjectNo,SubjectName,ClassHour,GradeID) VALUES (null,?,?,?);",
                subject.getSubjectName(), subject.getClassHour(), subject.getGradeID());
    }

    @Override
    public int deleteSubjectViaSubjectName(String SubjectName) {
        return baseUpdate("DELETE FROM subject WHERE SubjectName=?;", SubjectName);
    }

    @Override
    public int updateSubjectViaSubjectNo(Subject newSubject) {
        return baseUpdate("UPDATE subject SET SubjectName=?,ClassHour=?,GradeID=? WHERE SubjectNo=?;",
                newSubject.getSubjectName(), newSubject.getClassHour(), newSubject.getSubjectNo(), newSubject.getSubjectNo());
    }


    public static void main(String[] args) {
        new SubjectDaoImpl().getSubjectList().forEach(System.out::println);
        System.out.println("---------------------");

        Subject subject = new Subject();
        subject.setSubjectName("网络工程师");
        subject.setGradeID(4);
        subject.setClassHour(4);
        System.out.println(new SubjectDaoImpl().addSubject(subject));
        new SubjectDaoImpl().getSubjectList().forEach(System.out::println);
        System.out.println("---------------------");

        subject.setClassHour(400);
        System.out.println(new SubjectDaoImpl().updateSubjectViaSubjectNo(subject));
        new SubjectDaoImpl().getSubjectList().forEach(System.out::println);
        System.out.println("---------------------");

        System.out.println(new SubjectDaoImpl().deleteSubjectViaSubjectName("网络工程师"));
        new SubjectDaoImpl().getSubjectList().forEach(System.out::println);
        System.out.println("---------------------");
    }
}
